package cn.ecut.session;

import cn.ecut.User;
import cn.ecut.response.HeaderServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet("/urlRewrite")
public class URLRewriteServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 收集用户信息
        User user = new User() ;
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        user.setName( name );
        user.setPassword( password );
        // 理论上说，应该利用 JDBC 技术，去数据库中查找。但是这里做了一次简化

        // 利用 HttpServletRequest 获取 session
        HttpSession session = request.getSession();
        // 进行操作session
        session.setAttribute("user" , user );

        StringBuffer requestURL = request.getRequestURL();
        System.out.println( requestURL );
        String s = response.encodeRedirectURL("/success");
        System.out.println( s );
        response.sendRedirect( s ) ;
    }
}
